package com.woniu.adopt.web;

import com.github.pagehelper.PageInfo;
import com.woniu.adopt.common.ResultInfo;
import com.woniu.adopt.entity.PetArticle;
import com.woniu.adopt.result.ResultResponse;
import com.woniu.adopt.service.PetArticleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;


@Api(value = "本接口是宠物知识文章的功能", description = "宠物知识模块")
@RestController
@RequestMapping("petArticle")
public class PetArticleController implements ResultInfo {
    @Autowired
    PetArticleService petArticleService;

    @ApiOperation(value = "宠物知识文章的分页列表")
    @GetMapping("/list")
    public ResultResponse<PageInfo<PetArticle>> list(
            @RequestParam(value = "pageNum", defaultValue = "1", required = false) int pageNum,
            @RequestParam(value = "pageSize", defaultValue = "3", required = false) int pageSize,
            @RequestParam(value = "title", defaultValue = "", required = false) String title) {

        ResultResponse<PageInfo<PetArticle>> result = null;
        try {
            PageInfo<PetArticle> petArticlePageInfo = petArticleService.queryByPage(pageNum, pageSize, title);
            result = new ResultResponse<>(ResultInfo.SUCCESS_CODE, "查询宠物知识列表成功", petArticlePageInfo);

        } catch (RuntimeException e) {
            e.printStackTrace();
            result = new ResultResponse<>(ResultInfo.SYSTEM_ERROR_CODE, "查询宠物知识列表异常");
        }

        return result;
    }

    @ApiOperation(value = "根据id查询单个宠物知识文章")
    @GetMapping("/{id}")
    public ResultResponse<PetArticle> queryById(@PathVariable("id") int id) {
        ResultResponse<PetArticle> result = null;
        try {
            PetArticle petArticle = petArticleService.queryById(id);
            if (petArticle != null) {
                result = new ResultResponse<>(ResultInfo.SUCCESS_CODE, "查询宠物知识成功，浏览量更新成功", petArticle);
            } else {
                result = new ResultResponse<>(ResultInfo.FAILURE_CODE, "查询宠物知识失败，id不存在", petArticle);
            }
        } catch (RuntimeException e) {
            e.printStackTrace();
            result = new ResultResponse<>(ResultInfo.SYSTEM_ERROR_CODE, "查询宠物知识异常");
        }
        return result;
    }

    @ApiOperation(value = "新增宠物知识文章")
    @PostMapping("")
    public ResultResponse<Void> create(@RequestBody PetArticle petArticle
    ) {
        ResultResponse<Void> result = null;
        try {
            Date date = new Date();
            petArticle.setInsertTime(date);
            petArticle.setUpdateTime(date);
            petArticle.setViews(0L);
            petArticle.setDel(0);
            int i = petArticleService.add(petArticle);
            if (i == 1) {
                result = new ResultResponse<>(ResultInfo.SUCCESS_CODE, "新增宠物知识成功");
            } else {
                result = new ResultResponse<>(ResultInfo.FAILURE_CODE, "新增宠物知识失败");
            }
        } catch (RuntimeException e) {
            e.printStackTrace();
            result = new ResultResponse<>(ResultInfo.SYSTEM_ERROR_CODE, "新增宠物知识异常");
        }
        return result;

    }

    @ApiOperation(value = "删除宠物知识文章")
    @DeleteMapping("/{id}")
    public ResultResponse<Void> removeById(@PathVariable("id") int id) {
        ResultResponse<Void> result = null;
        try {
            int i = petArticleService.removeByid(id);
            if (i == 1) {
                result = new ResultResponse<>(ResultInfo.SUCCESS_CODE, "删除宠物知识成功");
            } else {
                result = new ResultResponse<>(ResultInfo.FAILURE_CODE, "删除宠物知识失败");
            }
        } catch (RuntimeException e) {
            e.printStackTrace();
            result = new ResultResponse<>(ResultInfo.SYSTEM_ERROR_CODE, "删除宠物知识异常");
        }
        return result;

    }

    @ApiOperation(value = "修改宠物知识文章")
    @PutMapping("/{id}")
    public ResultResponse<Void> modifyById(
            @PathVariable("id") int id, @RequestBody PetArticle petArticle
    ) {
        ResultResponse<Void> result = null;
        try {
            int i = petArticleService.modifyById(id, petArticle.getTitle(), petArticle.getImage(), petArticle.getIntroduce(), petArticle.getBody(), petArticle.getDisplay());
            if (i == 1) {
                result = new ResultResponse<>(ResultInfo.SUCCESS_CODE, "修改宠物知识成功");
            } else {
                result = new ResultResponse<>(ResultInfo.FAILURE_CODE, "修改宠物知识失败");
            }
        } catch (RuntimeException e) {
            e.printStackTrace();
            result = new ResultResponse<>(ResultInfo.SYSTEM_ERROR_CODE, "修改宠物知识异常");
        }
        return result;
    }


}
